import cv2
import pyautogui
from ultralytics import YOLO
import time
import sys
import os
import subprocess  # 确保导入 subprocess 模块
from moveFb模块 import movefb
import win32gui
import 检测框模块
from find_class_module import find_class_and_perform_action
from yolo2.find_and_perform_action_module import find_and_perform_action
from yolo2.mouse_move_module import move_mouse_to_target_class
from yolo2.yolo找图模块 import click_target_image
from yolo2.循环找图模块 import run_action_condition_found, run_action_condition_not_found
import subprocess
import sys

if hasattr(检测框模块, 'MODULE_LOADED') and 检测框模块.MODULE_LOADED:
    print("检测框模块已正确加载")
else:
    print("检测框模块加载失败")

def draw_detection_boxes(screenshot, results):
    for result in results:
        boxes = result.boxes.cpu().numpy()
        for box in boxes:
            class_id = int(box.cls[0])
            x1, y1, x2, y2 = box.xyxy[0].astype(int)
            center_x = int((x1 + x2) / 2)
            center_y = int((y1 + y2) / 2)

            # 在图像上绘制检测框
            cv2.rectangle(screenshot, (x1, y1), (x2, y2), (0, 255, 0), 2)
            cv2.putText(screenshot, f"Class {class_id}", (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)

    return screenshot

# 禁用 pyautogui 的安全保护机制
pyautogui.FAILSAFE = False

# 加载 YOLOv8n 模型并指定使用GPU
model = YOLO(r"h:\yol\yolo\v11\runs\detect\train31\weights\best.pt").to('cuda')

hwnd = 检测框模块.find_window()
if hwnd is None:
    print("未找到标题中包含'梦幻西游'的窗口")
else:
    left, top, right, bottom = win32gui.GetWindowRect(hwnd)
    window_width = right - left
    window_height = bottom - top


    def action_a():
        move_mouse_to_target_class(hwnd, model, 89, -50, 0, 0.3)
        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=89, offset_x=-20,
           offset_y=-10, 置信度=0.3, click_type='left')
        time.sleep(3)
    find_class_and_perform_action(hwnd, model, 5, action_a)

    def action_a():
         movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-100,
           offset_y=50, 置信度=0.3, click_type='left', )
    find_class_and_perform_action(hwnd, model, 148, action_a)
    movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=148, offset_x=-10,
           offset_y=-10, 置信度=0.3, click_type='left', )

    def action_a():

         movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=49, offset_x=-10,
           offset_y=-10, 置信度=0.3, click_type='left', )
    find_class_and_perform_action(hwnd, model, 1, action_a)


    def action_a():

         click_target_image(r"h:\zdh\yolo2\png\mydyt.png", offset_x=0, offset_y=-40, click_type='left')
    find_class_and_perform_action(hwnd, model, 1, action_a)



    def action_A():  # 调用找不到某图就循环执行动作1，否则执行动作2然后退出的功能
        print("执行 A 动作")
        def action():  # 找到红火就打绿火
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=40, offset_x=0,
                   offset_y=-10, 置信度=0.3, click_type='left', success_image=9, 如果不成功循环执行的次数=1)

        find_and_perform_action(hwnd, model, 57, action)

        def action():  # 找到红土就打绿土
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=38, offset_x=0,
                   offset_y=-10, 置信度=0.3, click_type='left', success_image=9, 如果不成功循环执行的次数=1)

        find_and_perform_action(hwnd, model, 41, action)

        def action():  # 找到红木就打绿木
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=39, offset_x=0,
                   offset_y=-10, 置信度=0.3, click_type='left', success_image=9, 如果不成功循环执行的次数=1)

        find_and_perform_action(hwnd, model, 58, action)

        def action():  # 找到红水打绿水
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=37, offset_x=0,
                   offset_y=-10, 置信度=0.3, click_type='left', success_image=9, 如果不成功循环执行的次数=1)

        find_and_perform_action(hwnd, model, 59, action)

        def action():  # 找到红水打绿水
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=22, offset_x=0,
                   offset_y=-10, 置信度=0.3, click_type='left', success_image=9, 如果不成功循环执行的次数=1)

        find_and_perform_action(hwnd, model, 56, action)
    find_class_and_perform_action(hwnd, model, 5, action_a)

    movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-10, offset_y=-10,
           置信度=0.3, click_type='left')
    subprocess.run([sys.executable, "主程序.py"], check=False)

    sys.exit()

    # 检测部分循环
    while True:
        # 重新获取窗口坐标和尺寸
        left, top, right, bottom = win32gui.GetWindowRect(hwnd)
        window_width = right - left
        window_height = bottom - top

        # 获取窗口截图
        screenshot = 检测框模块.get_window_screenshot(hwnd)

        # 进行目标检测
        results = model(screenshot)

        # 绘制检测框
        screenshot = draw_detection_boxes(screenshot, results)

        # 显示图像
        cv2.imshow("Detection Results", screenshot)


cv2.destroyAllWindows()